package com.xnfh.equipment.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.xnfh.common.bean.WeatherMessage
import com.xnfh.equipment.R
import com.xnfh.equipment.databinding.ActivityWeatherMessageLayoutBinding
import com.xnfh.equipment.viewmodel.WeatherMessageViewModel
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/27 19:02
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/27 19:02
 * @UpdateRemark: 更新说明
 */
class WeatherMessageActivity: BaseActivity<ActivityWeatherMessageLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_weather_message_layout
    companion object{
        fun start(context: Context, bundle: Bundle? = null){
            val intent = Intent(context, WeatherMessageActivity::class.java)
            bundle?.let { intent.putExtra("bundle",bundle) }
            context.startActivity(intent)
        }
    }

    private var type: String? = null
    private var viewModel: WeatherMessageViewModel? = null

    override fun init(savedInstanceState: Bundle?) {
        setHeaderTitle(getString(R.string.str_qx_message))
        val bundleExtra = intent.getBundleExtra("bundle")
        val id = bundleExtra?.getString("id")
        //5304是气象站 ,  5301是土壤水势
        type = bundleExtra?.getString("type")
        val name = bundleExtra?.getString("name")
        mBind.weatherTitle.text = name
        if (type == "5304") {
            mBind.weatherIcon.setImageResource(R.mipmap.qx_icon)
        } else {
            mBind.weatherIcon.setImageResource(R.mipmap.qx_icon_2)
        }
        viewModel?.requestWeatherMessage(id)
    }

    override fun observe() {
        viewModel = WeatherMessageViewModel()
        viewModel?.weatherMessage?.observe(this,object : BaseStateObserver<WeatherMessage>(){
            override fun getRespDataStart() {
                super.getRespDataStart()
                showLoadingDialog()
            }
            override fun getRespDataSuccess(it: WeatherMessage) {
                super.getRespDataSuccess(it)
                dismissLoadingDialog()
                if (type == "5304") {
                    mBind.weatherQx.visibility = View.VISIBLE
                    mBind.weatherSs.visibility = View.GONE
                    mBind.weatherOne.setValue(it.airTempHumidity)
                    mBind.weatherTwo.setValue(it.airPressure)
                    mBind.weatherThree.setValue(it.light)
                    mBind.weatherFour.setValue(it.rainfall)
                    mBind.weatherFive.setValue(it.soilTempMoisture1)
                    mBind.weatherSix.setValue(it.soilTempMoisture2)
                    mBind.weatherSeven.setValue(it.soilTempMoisture3)
                } else {
                    mBind.weatherQx.visibility = View.GONE
                    mBind.weatherSs.visibility = View.VISIBLE
                    mBind.weatherSs.setValue(it.SWP)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                dismissLoadingDialog()
            }
        })
    }
}